﻿using System;
using System.Web;
using System.Configuration;
using Eagle.Config;

namespace Eagle.Common
{
    /// <summary>
    /// Cookies操作类
    /// </summary>
    public class Cookies
    {
        #region 设置Cookies的值
        /// <summary>
        /// 设置单个Cookies的值，默认过期时间为浏览器进程结束
        /// </summary>
        /// <param name="keyName">cookies的键名称</param>
        /// <param name="value">cookies的值</param>
        public static void SetValue(string keyName, string value)
        {
            SetValue(keyName, "", value, 0, "", "", false, true);
        }
        /// <summary>
        /// 设置单个Cookies的值
        /// </summary>
        /// <param name="keyName">cookies的键名称</param>
        /// <param name="value">单个cookies的值</param>
        /// <param name="expires">有效期时间，以 秒 计算，例如：有效期是10分钟，则填写600秒，0表示浏览器关闭就失效，-1表示永久不过期</param>
        /// <param name="httponly">是否只允许http访问cookies,true-是（这样js就不能访问cookie），false-不是（js可以访问cookie）</param>
        public static void SetValue(string keyName, string value, int expires, bool httponly)
        {
            SetValue(keyName, "", value, expires, "", "", false, httponly);
        }
        /// <summary>
        /// 设置含有子键的Cookies的值
        /// </summary>
        /// <param name="fatherKey">cookie的父键名称</param>
        /// <param name="childKey">cookie的子键名称</param>
        /// <param name="value">cookie的子键值</param>
        public static void SetValue(string fatherKey, string childKey, string value)
        {
            SetValue(fatherKey, childKey, value, 0, "", "", false, true);
        }
           /// <summary>
        /// 设置含有子键的Cookies的值
        /// </summary>
        /// <param name="fatherKey">cookie的父键名称</param>
        /// <param name="childKey">cookie的子键名称</param>
        /// <param name="value">cookie的子键值</param>
        /// <param name="expires">有效期时间，以 秒 计算，例如：有效期是10分钟，则填写600秒，0表示浏览器关闭就失效，-1表示永久不过期</param>
        /// <param name="httponly">是否只允许http访问cookies,true-是（这样js就不能访问cookie），false-不是（js可以访问cookie）</param>
        public static void SetValue(string fatherKey, string childKey, string value, int expires,  bool httponly)
        {
              SetValue(fatherKey, childKey, value, expires, "", "", false, httponly);
        }
        /// <summary>
        /// 设置含有子键的Cookies的值
        /// </summary>
        /// <param name="fatherKey">cookie的父键名称</param>
        /// <param name="childKey">cookie的子键名称</param>
        /// <param name="value">cookie的子键值</param>
        /// <param name="expires">有效期时间，以 秒 计算，例如：有效期是10分钟，则填写600秒，0表示浏览器关闭就失效，-1表示永久不过期</param>
        /// <param name="domain">cookie的域</param>
        /// <param name="path">cookie的路径</param>
        /// <param name="security">是否只使用HTTPS，true-是，false-否</param>
        /// <param name="httponly">是否只允许http访问cookies,true-是（这样js就不能访问cookie），false-不是（js可以访问cookie）</param>
        public static void SetValue(string fatherKey, string childKey, string value, int expires, string domain, string path, bool security, bool httponly)
        {
            HttpCookie httpcookies = HttpContext.Current.Request.Cookies[fatherKey];
            if (httpcookies == null)
                httpcookies = new HttpCookie(fatherKey);
            if (childKey.Length > 0)
            {
                httpcookies.Values[childKey] = HttpUtility.UrlEncode(value);
            }
            else
            {
                httpcookies.Value = HttpUtility.UrlEncode(value);
            }
            if (domain.Length == 0)
                domain = EagleSettings.Instance.CookiesDomain;
            if (path.Length == 0)
                path = "/";
            if (expires.Equals(-1))
            { httpcookies.Expires = DateTime.MaxValue; }
            else if (expires > 0)
            { httpcookies.Expires = DateTime.Now.AddSeconds(expires); }
            httpcookies.Domain = domain;
            httpcookies.Path = path;
            httpcookies.Secure = security;
            httpcookies.HttpOnly = httponly;
            HttpContext.Current.Response.Cookies.Add(httpcookies);
        }
        #endregion

        #region 获取Cookies的值
        /// <summary>
        /// 获取Cookies的值,返回string类型,没有值将返回空字符串（string.Empty）
        /// </summary>
        /// <param name="keyName">要获取的cookie的键名称</param>
        /// <returns>返回字符串</returns>
        public static string GetValue(string keyName)
        {
            return GetValue(keyName, "");
        }
        /// <summary>
        /// 获取Cookies的值,返回string类型，没有值将返回空字符串（string.Empty）
        /// </summary>
        /// <param name="fatherKey">要获取的cookie的父键名称</param>
        /// <param name="childKey">要获取的cookie的子键名称</param>
        /// <returns>返回字符串</returns>
        public static string GetValue(string fatherKey, string childKey)
        {
            HttpCookie httpCookie = HttpContext.Current.Request.Cookies[fatherKey];
            if (httpCookie != null)
            {
                if (childKey.Length == 0)//如果没有子键名称，则说明要取单值cookies
                {
                    return HttpUtility.UrlDecode(httpCookie.Value);
                }
                else
                {
                    if (httpCookie[childKey] != null)
                    {
                        return HttpUtility.UrlDecode(httpCookie[childKey]);
                    }
                }
            }
            return string.Empty;
        }
        #endregion

        #region 用户退出删除cookies
        /// <summary>
        /// 用户退出删除cookies
        /// </summary>
        /// <param name="cookiesNameArray">Cookie名称集合</param>
        public static void DeleteCookies(string[] cookiesNameArray)
        {
            foreach (string cookieName in cookiesNameArray)
            {
                HttpCookie myCookie = HttpContext.Current.Request.Cookies[cookieName];
                if (myCookie == null)
                    myCookie = new HttpCookie(cookieName);
                myCookie.Values.Clear();
                myCookie.Expires = DateTime.Now.AddYears(-1);
                //以下这一句非常关键，如果登录时使用了domain，那么退出时也需要指定cookies的domain。
                myCookie.Domain = EagleSettings.Instance.CookiesDomain;
                HttpContext.Current.Response.AppendCookie(myCookie);
            }
        }
        /// <summary>
        /// 用户退出删除cookies
        /// </summary>
        /// <param name="cookiesName">Cookie名称集合</param>
        public static void DeleteCookies(string cookiesName)
        {
            DeleteCookies(new string[] { cookiesName });
        }
        #endregion

    }
}
